package com.zb.emq;

import com.zb.entity.TbGpsModel;
import com.zb.entity.TbQuotaModel;
import com.zb.service.TbGpsService;
import com.zb.service.TbQuotaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 项目启动自定触发，初始化方法
 */
@Component
@Slf4j
public class Monitor {

    @Autowired
    private EmqClient emqClient;

    @Autowired
    private TbQuotaService quotaService;
    @Autowired
    private TbGpsService gpsService;

    @PostConstruct
    public void init() {
        log.info("项目初始化！");
        try {
            emqClient.connect();
            /*订阅报警 的指标*/
            List<TbQuotaModel> list = quotaService.list();
            for (TbQuotaModel tbQuotaModel : list) {
                emqClient.subscribe(tbQuotaModel.getSubject());
            }
            /*订阅gps*/
            List<TbGpsModel> gpsModelList = gpsService.list();
            for (TbGpsModel gpsModel : gpsModelList) {
                emqClient.subscribe(gpsModel.getSubject());
            }
            log.info("主题订阅成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
